package com.jic.market.impl;


import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.market.common.base.MarketBaseService;
import com.jic.market.common.enums.*;
import com.jic.market.common.util.ListUtils;
import com.jic.market.entity.*;
import com.jic.market.enums.PromotionStatusEnum;
import com.jic.market.enums.PromotionTypeEnum;
import com.jic.market.mapper.*;
import com.jic.market.orm.Pagination;
import com.jic.market.request.PromotionLimitRuleRequest;
import com.jic.market.request.PromotionProductQueryListRequest;
import com.jic.market.request.PromotionProductSaveBatchListRequest;
import com.jic.market.request.PromotionProductSaveBatchRequest;
import com.jic.market.request.manager.goods.GoodsSkuQueryRequest;
import com.jic.market.request.manager.single.PromotionSingleCopyRequest;
import com.jic.market.response.PageResponse;
import com.jic.market.response.PromotionProductResponse;
import com.jic.market.response.PromotionProductSonResponse;
import com.jic.market.response.manager.goods.GoodsSkuInfo;
import com.jic.market.response.manager.goods.GoodsSpuInfo;
import com.jic.market.response.manager.goods.QueryGoodsInfoResponse;
import com.jic.market.service.*;
import com.jic.product.after.api.ProductListApi;
import com.jic.product.after.request.GoodsPageQueryRequest;
import com.jic.product.after.response.ProductStoreInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author zxl
 * @date: 2020年03月11日
 */
@Service
@Slf4j
public class PromotionSingleRuleServiceImpl extends MarketBaseService implements PromotionSingleLimitRuleService {

    @Autowired
    PromotionSingleRuleEntityMapper promotionSingleRuleEntityMapper;
    @Autowired
    private PromotionEntityMapper promotionEntityMapper;
    @Autowired
    private PromotionMerchantEntityMapper promotionMerchantEntityMapper;
    @Autowired
    private PromotionMerchantProductEntityMapper promotionMerchantProductEntityMapper;
    @Autowired
    private PromotionLimitRuleEntityMapper promotionLimitRuleEntityMapper;
    @Autowired
    private PromotionTemplateService promotionTemplateService;
    @Resource
    private PromotionMerchantProductService pmpService;
    @Resource
    PromotionLimitRuleService limitService;
    @Resource
    private PromotionSingleLimitRuleService promotionSingleLimitRuleService;
    @Resource
    private PromotionMutexService promotionMutexService;

    @Resource
    private ProductListApi productListApi;


    @Override
    public void updatePriceOrLimit(PromotionLimitRuleRequest ruleRequest) {
        PromotionSingleRuleEntity limitSingleRuleEntity = new PromotionSingleRuleEntity();
        BeanUtils.copyProperties(ruleRequest, limitSingleRuleEntity);
        limitSingleRuleEntity.setProductId(ruleRequest.getMpId());
        limitSingleRuleEntity.setContentValue(ruleRequest.getContentValue());
        promotionSingleRuleEntityMapper.updateLimit(limitSingleRuleEntity);
    }

    @Override
    public void delete(List<Long> ids) {
        ids.forEach(x->{
            promotionSingleRuleEntityMapper.delete(x);
        });
    }

    @Override
    public Integer addPromotionSingleLimitRule(PromotionSingleRuleEntity PromotionSingleLimitRule) {
        return promotionSingleRuleEntityMapper.insert(PromotionSingleLimitRule);
    }

    @Override
    public PromotionSingleRuleEntity getEntityByPromotionIdAndMpId(Long promotionId, Long productId) {

        return promotionSingleRuleEntityMapper.getEntityByPromotionIdAndMpId(promotionId, productId);
    }


    @Override
    public Integer deleteEntityByPromotionIdAndMpId(Long promotionId, Long productId) {
        promotionSingleRuleEntityMapper.deleteEntityByPromotionIdAndMpId(promotionId, productId);
        return null;
    }

    @Override
    public RestResult<Long> copy(PromotionSingleCopyRequest request) {
        return executeQuery(() -> {
            // 复制配置信息，返回新的活动id
            PromotionTemplateService.CopyResult copyResult = copyConfig(request, PromotionStatusEnum.DRAFT, "-复制");
            if(null != copyResult){
                return Long.valueOf(copyResult.getPromotionId());
            }
            return null;
        });
    }

    /*
    *  保存为模板
    * */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public RestResult<Long> saveTemplate(PromotionSingleCopyRequest request) {
        return executeQuery(() -> {
            Long templateId = promotionTemplateService.saveTemplate(() -> {
                // 复制配置信息
                PromotionTemplateService.CopyResult copyResult = copyConfig( request, PromotionStatusEnum.TEMPLATE, "-模板");
                copyResult.setAdminOperate(request.getAdminOperate());
                return copyResult;
            });
            return templateId;
        });
    }


    /**
     * 复制方法
     * @param oldPromotion 被复制的活动id
     * @param status 复制需要存储的状态
     * @param extenPromName 对应的活动扩展名
     * @return
     */
    private PromotionTemplateService.CopyResult copyConfig(PromotionSingleCopyRequest oldPromotion, PromotionStatusEnum status, String extenPromName) {
        // 复制主表 promotion
        PromotionEntity promotionEntityQ = new PromotionEntity();
        promotionEntityQ.setId(oldPromotion.getPromotionId());
        //查询主表
        PromotionEntity promotionEntity = promotionEntityMapper.query(promotionEntityQ);
        if(null == promotionEntity) {
            return null;
        }
        promotionEntity.setId(null);
        promotionEntity.setStatus(status.getCode());
        promotionEntity.setPromName(promotionEntity.getPromName() + extenPromName);
        promotionEntity.setCreateTime(new Date());
        promotionEntity.setUpdateTime(new Date());
        //插入查询主表
        int flag = promotionEntityMapper.insert(promotionEntity);
        if(0 == flag) {
            return null;
        }
        //结果组装 enum
        PromotionTemplateService.CopyResult copyResult = new PromotionTemplateService.CopyResult();
        copyResult.setPromotionId(promotionEntity.getId() + "");
        copyResult.setPromType(PromotionTypeEnum.getByCode(promotionEntity.getPromType()));
        copyResult.setTemplateName(oldPromotion.getTemplateName());

        // 复制规则配置表 promotion_limit_rule
        List<PromotionLimitRuleEntity> promotionLimitRuleList =  promotionLimitRuleEntityMapper.queryByPromotionId(oldPromotion.getPromotionId());
        if(null != promotionLimitRuleList && !promotionLimitRuleList.isEmpty()){
            for (int i = 0; i < promotionLimitRuleList.size();i++){
                promotionLimitRuleList.get(i).setPromotionId(promotionEntity.getId());
                promotionLimitRuleList.get(i).setId(null);
                promotionLimitRuleEntityMapper.insert(promotionLimitRuleList.get(i));
            }

        }

        // 复制商家店铺表 promotion_merchant
        PromotionMerchantEntity merchantEntityQ = new PromotionMerchantEntity();
        merchantEntityQ.setPromotionId(oldPromotion.getPromotionId());
        List<PromotionMerchantEntity> promotionMerchantEntityList = promotionMerchantEntityMapper.findAll(merchantEntityQ);
        if(null != promotionMerchantEntityList && !promotionMerchantEntityList.isEmpty()){
            for(PromotionMerchantEntity merchantEntity: promotionMerchantEntityList){
                merchantEntity.setId(null);
                merchantEntity.setPromotionId(promotionEntity.getId());
                merchantEntity.setCreateTime(new Date());
                merchantEntity.setUpdateTime(new Date());
                promotionMerchantEntityMapper.insert(merchantEntity);
            }
        }

        // 复制商品表 promotion_merchant_product
        PromotionMerchantProductEntity promotionMerchantProduct = new PromotionMerchantProductEntity();
        promotionMerchantProduct.setPromotionId(oldPromotion.getPromotionId());
        List<PromotionMerchantProductEntity> promotionMerchantProductList = promotionMerchantProductEntityMapper.queryPromotionMerchantProduct(promotionMerchantProduct);
        if(null != promotionMerchantProductList && !promotionMerchantProductList.isEmpty()){
            for(PromotionMerchantProductEntity promotionMerchantNew : promotionMerchantProductList){
                promotionMerchantNew.setId(null);
                promotionMerchantNew.setPromotionId(promotionEntity.getId());
                promotionMerchantNew.setCreateTime(new Date());
                promotionMerchantNew.setUpdateTime(new Date());
                promotionMerchantProductEntityMapper.insert(promotionMerchantNew);
            }
        }

        // 单一促销代理商规则表 promotion_single_rule
        List<PromotionSingleRuleEntity> promotionSingleRuleList = promotionSingleRuleEntityMapper.queryPromotionSingleRuleById(oldPromotion.getPromotionId());
        if(null != promotionSingleRuleList && !promotionSingleRuleList.isEmpty()){
            for(PromotionSingleRuleEntity promotionSingle : promotionSingleRuleList){
                promotionSingle.setId(null);
                promotionSingle.setPromotionId(promotionEntity.getId());
                promotionSingle.setCreateTime(new Date());
                promotionSingle.setUpdateTime(new Date());
                promotionSingleRuleEntityMapper.insert(promotionSingle);
            }
        }
        return copyResult;
    }


    /**
     * 删除方法方法
     * @param promotionId 活动id
     * @param
     * @return
     */
    @Override
    public RestResult deletePromotionSingle(Long promotionId) {
        // 删除 主表 promotion
         promotionEntityMapper.delete(promotionId);
        // 删除 规则配置表 promotion_limit_rule
         promotionLimitRuleEntityMapper.deleteEntityByPromotionIdAndMpId(promotionId,null);
        // 删除 商家店铺表 promotion_merchant
         promotionMerchantEntityMapper.deleteByPromotionId(promotionId);
        // 删除 商品表 promotion_merchant_product
         promotionMerchantProductEntityMapper.deleteEntityByPromotionIdAndMpId(promotionId,null);
        // 删除 单一促销代理商规则表 promotion_single_rule
        promotionSingleRuleEntityMapper.deleteEntityByPromotionIdAndMpId(promotionId,null);
        return null;
    }

    @Override
    public PageResult<PromotionProductResponse> getPromotionProductList(PromotionProductQueryListRequest ppqlRequest) {
                PageResult pageResult =new PageResult();
                PromotionMerchantProductEntity condition =new PromotionMerchantProductEntity(); //实体类
                BeanUtils.copyProperties(ppqlRequest,condition); //将参数复制到类中
                Pagination pagination =new Pagination(); //当前页 每页个数 总条数
                pagination.setLimitStart((ppqlRequest.getStart()-1) * ppqlRequest.getLimit()); //总条数
                pagination.setPageSize(ppqlRequest.getLimit()); //每页个数
                condition.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode()); //是否可用
                condition.setCreatorName(null); //创建人的姓名
                condition.setCreator(null); //创建人
                condition.setUpdator(null); //更新人
                condition.setUpdatorName(null); //更新人姓名
                // 只查一层
                condition.setMpParentId(0L);  //父商品的id
                 //根据实体类和规格查询查询出PromotionMerchantProduct表数据
                List<PromotionMerchantProductEntity> productList = pmpService.findOfPage(condition, pagination);
                //查询出整张表的促销商品的总数
                Integer count = pmpService.countOfPage(condition);
                List<PromotionProductResponse> responseList = null;
                if(productList !=null && !productList.isEmpty()){
                    responseList = ListUtils.entityListToModelList(productList, PromotionProductResponse.class); //将entityList转换成modelList
                }
                if(responseList !=null && !responseList.isEmpty()){
                    for (PromotionProductResponse pp : responseList) {

                        PromotionMerchantProductEntity conditionSon =new PromotionMerchantProductEntity(); //实体类
                        BeanUtils.copyProperties(ppqlRequest,conditionSon); //将接收的参数复制到实体类中
                        Pagination paginationSon =new Pagination(); //
                        paginationSon.setLimitStart(0); //总条数
                        paginationSon.setPageSize(10); //每页个数
                        conditionSon.setPromotionId(ppqlRequest.getPromotionId()); //促销活动id
                        conditionSon.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode()); //是否可用

                        conditionSon.setMpParentId(pp.getMpId()); // 父商品id
                        //根据实体类和条件查询出所有数据
                        List<PromotionMerchantProductEntity> productListSon = pmpService.findOfPage(conditionSon, paginationSon); //查询

                        List<PromotionProductSonResponse> responseListSon = null;
                        if (productListSon !=null && !productListSon.isEmpty()){
                            //将productListSon转换成PromotionProductSonResponse
                            responseListSon = ListUtils.entityListToModelList(productListSon, PromotionProductSonResponse.class);
                        }

                        if(responseListSon !=null && !responseListSon.isEmpty()){
                            for (PromotionProductSonResponse ppSon : responseListSon) {
                                //根据促销活动id和商家商品id查询
                                PromotionLimitRuleEntity promotionLimitRuleSon = limitService.getEntityByPromotionIdAndMpId(ppSon.getPromotionId(), ppSon.getMpId());
                                if(promotionLimitRuleSon !=null){
                                    ppSon.setPlatformTotalLimit(promotionLimitRuleSon.getPlatformTotalLimit()); //平台总限量
                                    ppSon.setChannelMerchantLimit(promotionLimitRuleSon.getChannelMerchantLimit()); //单渠道商家限制
                                    ppSon.setChannelStoreLimit(promotionLimitRuleSon.getChannelStoreLimit()); //单渠道门店限制
                                    ppSon.setChannelIndividualLimit(promotionLimitRuleSon.getChannelIndividualLimit()); //单渠道个人限制
                                    ppSon.setTotalLimit(promotionLimitRuleSon.getTotalLimit()); //全网限制数量总数
                                    ppSon.setIndividualLimit(promotionLimitRuleSon.getIndividualLimit()); //个人限制数量
                                    ppSon.setTotalLimitAmount(promotionLimitRuleSon.getTotalLimitAmount()); //全网限制优惠金额
                                    ppSon.setOrderLimit(promotionLimitRuleSon.getOrderLimit()); //每单限量
                                }
                            }
                        }

                        pp.setChildren(responseListSon);
                        // 查询 promotion_limit_rule 表
//                        PromotionLimitRuleEntity promotionLimitRule = limitService.getEntityByPromotionIdAndMpId(pp.getPromotionId(), pp.getMpId());
//                        // 查询 promotion_single_rule 表
//                        PromotionSingleRuleEntity promotionSingleValue = promotionSingleLimitRuleService.getEntityByPromotionIdAndMpId(pp.getPromotionId(), pp.getMpId());
//                        if (promotionLimitRule !=null && promotionSingleValue !=null){
//                                pp.setPlatformTotalLimit(promotionLimitRule.getPlatformTotalLimit());
//                                pp.setChannelMerchantLimit(promotionLimitRule.getChannelMerchantLimit());
//                                pp.setChannelStoreLimit(promotionLimitRule.getChannelStoreLimit());
//                                pp.setChannelIndividualLimit(promotionLimitRule.getChannelIndividualLimit());
//                                pp.setTotalLimit(promotionLimitRule.getTotalLimit());
//                                pp.setIndividualLimit(promotionLimitRule.getIndividualLimit());
//                                pp.setTotalLimitAmount(promotionLimitRule.getTotalLimitAmount());
//                                pp.setOrderLimit(promotionLimitRule.getOrderLimit());
//                               //特价、折扣、直降
//                                pp.setContentValue(promotionSingleValue.getContentValue());
//                        }

//                        if (MutexFlagEnum.MUTEX.getCode().equals(ppqlRequest.getMutexFlag())){
//                            // 根据促销 id，和商品 id 查看该商品互斥活动
//                            PromotionMutexEntity conditionMutex =new PromotionMutexEntity();
//                            conditionMutex.setPromotionId(pp.getPromotionId());
//                            conditionMutex.setPromotionMerchantProductId(pp.getId());
//                            conditionMutex.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode());
//                            PromotionMutexEntity promotionMutexEntity =
//                                    promotionMutexService.getPromotionMutexEntity(conditionMutex);
//                            if(promotionMutexEntity !=null){
//                                //设置互斥记录时间
//                                pp.setMutexTime(promotionMutexEntity.getCreateTime());
//                            }
//                        }
                        pageResult.setRows(responseList);
                        pageResult.setTotal(count);
                    }
                }
        return pageResult;
      }



    /*
    * 单一活动批量保存
    *
    * */
    @Override
    public Integer saveSingleMerchantProduct(PromotionProductSaveBatchListRequest request) {
        List<PromotionProductSaveBatchRequest> batchSpuRequestList = request.getProductPromotionList(); //商品的spuid和促销id
        if(CollectionUtils.isEmpty(batchSpuRequestList)){ //判断是否为空
            return null;
        }
        final Long promotionId = batchSpuRequestList.get(0).getPromotionId(); //不为空 就获取它的id
        final List<PromotionMerchantProductEntity> productEntities = new ArrayList<>(); //创建一个集合 返回类型是PromotionMerchantProductEntity

        List<Long> spuIds = batchSpuRequestList.stream().map(PromotionProductSaveBatchRequest::getSpuId).collect(Collectors.toList());
        GoodsPageQueryRequest goodsSkuQueryRequest = new GoodsPageQueryRequest();
        goodsSkuQueryRequest.setSpuIds(spuIds);
//        RestResult<List<QueryGoodsInfoResponse>> listRestResult = goodsQueryService.queryByGoodsSpuIds(goodsSkuQueryRequest);

        List<ProductStoreInfo> goodsInfoResponseList = productListApi.queryProductBySpuId(goodsSkuQueryRequest);

        if(CollectionUtils.isEmpty(goodsInfoResponseList)){
            return null;
        }

        goodsInfoResponseList.forEach(goodsInfo->{
            ProductStoreInfo goodsSpuInfo = goodsInfo;
            List<ProductStoreInfo> goodsSkuInfoList = goodsInfo.getChildren();
            //SPU
            PromotionMerchantProductEntity spuParam = new PromotionMerchantProductEntity();
            spuParam.setMpId(goodsSpuInfo.getId());
            spuParam.setPromotionId(promotionId);
            //spuParam.setStoreId(goodsSpuInfo.getStoreId());
            spuParam.setTypeOfProduct(TypeOfProductEnum.GOODS_SPU.getCode());
            List<PromotionMerchantProductEntity> spuList = promotionMerchantProductEntityMapper.findAll(spuParam);
            if(CollectionUtils.isNotEmpty(spuList)){
                spuList.forEach(s->{
                    if(s.getDeleteFlag() == 1){
                        PromotionMerchantProductEntity updateParam = new PromotionMerchantProductEntity();
                        updateParam.setId(s.getId());
                        updateParam.setDeleteFlag(0);
                        promotionMerchantProductEntityMapper.update(updateParam);
                    }
                });
            }else{
                //组装entity
                PromotionMerchantProductEntity entity = new PromotionMerchantProductEntity();
                entity.setMpId(goodsSpuInfo.getId());
                entity.setPromotionId(promotionId);
                entity.setMpName(goodsSpuInfo.getChineseName());
                entity.setMpParentId(0L);
                entity.setTypeOfProduct(TypeOfProductEnum.GOODS_SPU.getCode());
                entity.setCategoryName(goodsSpuInfo.getCategoryName());
                entity.setScopeType(ProductScopeTypeEnum.PRODUCT.getCode());
                entity.setMutexFlag(MutexFlagEnum.UN_MUTEX.getCode());
                entity.setStatus(0);
                entity.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode());
                entity.setStoreId(goodsSpuInfo.getStoreId());
                entity.setMerchantId(goodsSpuInfo.getMerchantId());
                entity.setCreator(request.getCreator());
                entity.setUpdator(request.getUpdator());
                entity.setCreateTime(new Date());
                entity.setUpdatorName(request.getUpdatorName());
                entity.setUpdateTime(new Date());
                productEntities.add(entity);
            }

            //SKU
            if(CollectionUtils.isNotEmpty(goodsSkuInfoList)){
                goodsSkuInfoList.forEach(goodsSkuInfo -> {
                    PromotionMerchantProductEntity skuParam = new PromotionMerchantProductEntity();
                    skuParam.setMpId(goodsSkuInfo.getId());
                    skuParam.setPromotionId(promotionId);
                    //skuParam.setStoreId(goodsSkuInfo.getStoreId());
                    skuParam.setTypeOfProduct(TypeOfProductEnum.GOODS_SKU.getCode());
                    List<PromotionMerchantProductEntity> skuList = promotionMerchantProductEntityMapper.findAll(skuParam);
                    if(CollectionUtils.isNotEmpty(skuList)){
                        skuList.forEach(sk->{
                            if(sk.getDeleteFlag() == 1){
                                PromotionMerchantProductEntity updateParam = new PromotionMerchantProductEntity();
                                updateParam.setId(sk.getId());
                                updateParam.setDeleteFlag(0);
                                promotionMerchantProductEntityMapper.update(updateParam);
                            }
                        });
                    }else{
                        //组装entity
                        PromotionMerchantProductEntity entity = new PromotionMerchantProductEntity();
                        entity.setMpId(goodsSkuInfo.getId());
                        entity.setPromotionId(promotionId);
                        entity.setMpName(goodsSkuInfo.getChineseName());
                        entity.setTypeOfProduct(TypeOfProductEnum.GOODS_SKU.getCode());
                        entity.setMpParentId(goodsSpuInfo.getId());
                        entity.setCategoryName(goodsSkuInfo.getCategoryName());
                        entity.setMerchantId(goodsSkuInfo.getMerchantId());
                        entity.setScopeType(ProductScopeTypeEnum.PRODUCT.getCode());
                        entity.setStoreId(goodsSkuInfo.getStoreId());
                        entity.setMutexFlag(MutexFlagEnum.UN_MUTEX.getCode());
                        entity.setStatus(0);
                        entity.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode());
                        entity.setCreator(request.getCreator());
                        entity.setUpdator(request.getUpdator());
                        entity.setCreateTime(new Date());
                        entity.setUpdatorName(request.getUpdatorName());
                        entity.setUpdateTime(new Date());
                        productEntities.add(entity);
                    }
                });
            }
        });
        productEntities.forEach(promotionProduct->{
            promotionMerchantProductEntityMapper.insert(promotionProduct);
//            // 添加促销商品的限制项，
            // 用于添加到 promotion_limit_rule里，需要指定limit_type为GOODS:商品
//            log.info("----进入批量保存促销活动商品--添加促销商品的限制项--当前促销id:{}--",promotionId);
//            PromotionLimitRuleEntity queryPromotionLimitRuleEntity=new PromotionLimitRuleEntity();
//            queryPromotionLimitRuleEntity.setPromotionId(promotionProduct.getPromotionId());
//            queryPromotionLimitRuleEntity.setProductId(promotionProduct.getMpId());
//            queryPromotionLimitRuleEntity.setLimitType(5);
//            PromotionLimitRuleEntity dbEntity = promotionLimitRuleEntityMapper.queryPromotionLimitRuleByEntity(queryPromotionLimitRuleEntity);
//            if(dbEntity != null){
//                continue;
//            }
//            PromotionLimitRuleEntity limitEntity =new PromotionLimitRuleEntity();
//            limitEntity.setPromotionId(promotionProduct.getPromotionId());
//            limitEntity.setLimitType(LimitTypeEnum.GOODS.getCode());
//            // 之前是在修改 limitEntity中productId，现修改为 limitRef
//            limitEntity.setLimitRef(promotionProduct.getMpId());
//            limitEntity.setProductId(promotionProduct.getMpId());
//            limitEntity.setCreator(request.getCreator());
//            limitEntity.setCreatorName(request.getCreatorName());
//            limitEntity.setUpdator(request.getCreator());
//            limitEntity.setCreateTime(new Date());
//           limitService.addPromotionLimitRule(limitEntity);
//            // 添加单一促销商品的限制项，
//            // 用于添加到 promotion_single_rule
//            PromotionSingleRuleEntity limitEntitySingle =new PromotionSingleRuleEntity();
//            limitEntitySingle.setPromotionId(promotionProduct.getPromotionId());
//            limitEntitySingle.setProductId(promotionProduct.getMpId());
//            limitEntitySingle.setCreator(request.getCreator());
//            limitEntitySingle.setCreatorName(request.getCreatorName());
//            limitEntitySingle.setUpdator(request.getCreator());
//            limitEntitySingle.setCreateTime(new Date());
//            this.addPromotionSingleLimitRule(limitEntitySingle);

        });
        return productEntities.size();
    }


    /**
     * 插入促销商品限制
     * @param ruleRequest
     * @return
     */
    @Override
    public Long insertLimit(PromotionLimitRuleRequest ruleRequest) {
        //促销限制规则表
        PromotionLimitRuleEntity ruleEntity = new PromotionLimitRuleEntity();
        BeanUtils.copyProperties(ruleRequest, ruleEntity);
        ruleEntity.setLimitRef(ruleRequest.getMpId());
        ruleEntity.setLimitType(ruleRequest.getLimitType());
        //单一促销规则
        PromotionSingleRuleEntity limitEntitySingle =new PromotionSingleRuleEntity();
        limitEntitySingle.setPromotionId(ruleRequest.getPromotionId());
        limitEntitySingle.setProductId(ruleRequest.getMpId());
        limitEntitySingle.setContentValue(ruleRequest.getContentValue());
        limitEntitySingle.setCreator(ruleRequest.getCreator());
        limitEntitySingle.setCreatorName(ruleRequest.getCreatorName());
        limitEntitySingle.setUpdator(ruleRequest.getCreator());
        limitEntitySingle.setCreateTime(new Date());
        //根据promotion_limit_rule id 判断是否新增或者修改
        if(ruleEntity.getId() == null){
            int insert = promotionLimitRuleEntityMapper.insert(ruleEntity);
            limitService.addPromotionLimitRule(ruleEntity);
            this.addPromotionSingleLimitRule(limitEntitySingle);
        }else{
            promotionLimitRuleEntityMapper.update(ruleEntity);
        }
        return ruleEntity.getId();
    }

}
